{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第04课：了解数据必备的文本可视化技巧"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1）基于文本内容的可视化。\n",
    "\n",
    "基于文本内容的可视化研究包括基于词频的可视化和基于词汇分布的可视化，常用的有词云、分布图和 Document Cards 等。\n",
    "\n",
    "（2）基于文本关系的可视化。\n",
    "\n",
    "基于文本关系的可视化研究文本内外关系，帮助人们理解文本内容和发现规律。常用的可视化形式有树状图、节点连接的网络图、力导向图、叠式图和 Word Tree 等。\n",
    "\n",
    "（3）基于多层面信息的可视化\n",
    "\n",
    "基于多层面信息的可视化主要研究如何结合信息的多个方面帮助用户从更深层次理解文本数据，发现其内在规律。其中，包含时间信息和地理坐标的文本可视化近年来受到越来越多的关注。常用的有地理热力图、ThemeRiver、SparkClouds、TextFlow 和基于矩阵视图的情感分析可视化等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/zhangjianfeng/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:15: FutureWarning: using a dict on a Series for aggregation\n",
      "is deprecated and will be removed in a future version\n",
      "  from ipykernel import kernelapp as app\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from hiveplot import HivePlot\n",
    "import matplotlib.pyplot as plt \n",
    "%matplotlib inline\n",
    "import json\n",
    "from urllib.request import urlopen, quote\n",
    "import requests,csv\n",
    "import pandas as pd #导入这些库后边都要用到\n",
    "\n",
    "dir = \"../data/04/\"\n",
    "columns = ['std_id','sex','year','colloge','city']\n",
    "df = pd.read_csv(dir + \"nd_student_info.csv\",sep='\\t',names=columns)\n",
    "#分组\n",
    "cities_stat=df.groupby(by=['city'])['city'].agg({\"count\":np.size}) \n",
    "#排序\n",
    "cities_stat=cities_stat.reset_index().sort_values(by=[\"count\"],ascending=False)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "893"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(cities_stat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#经纬度转换参考https://www.jianshu.com/p/773ff5f08a2c\n",
    "\n",
    "def getlnglat(address):\n",
    "    url = 'http://api.map.baidu.com/geocoder/v2/'\n",
    "    output = 'json'\n",
    "    ak = 'sqGDDvCDEZPSz24bt4b0BpKLnMk1dv6M'\n",
    "    add = quote(address) #由于本文城市变量为中文，为防止乱码，先用quote进行编码\n",
    "    uri = url + '?' + 'address=' + add  + '&output=' + output + '&ak=' + ak\n",
    "    req = urlopen(uri)\n",
    "    res = req.read().decode() #将其他编码的字符串解码成unicode\n",
    "    temp = json.loads(res)  #对json数据进行解析\n",
    "    return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "116.39564503787867\n",
      "39.92998577808024\n",
      "{'city': '北京', 'lng': 116.39564503787867, 'lat': 39.92998577808024, 'count': 840}\n",
      "104.06792346330406\n",
      "30.679942845419564\n",
      "{'city': '成都', 'lng': 104.06792346330406, 'lat': 30.679942845419564, 'count': 291}\n",
      "106.53063501341296\n",
      "29.54460610888615\n",
      "{'city': '重庆', 'lng': 106.53063501341296, 'lat': 29.54460610888615, 'count': 261}\n",
      "102.71460113878045\n",
      "25.049153100453157\n",
      "{'city': '昆明', 'lng': 102.71460113878045, 'lat': 25.049153100453157, 'count': 238}\n",
      "119.14263382297052\n",
      "36.71611487305138\n",
      "{'city': '潍坊', 'lng': 119.14263382297052, 'lat': 36.71611487305138, 'count': 214}\n",
      "117.02496706629023\n",
      "36.68278472716141\n",
      "{'city': '济南', 'lng': 117.02496706629023, 'lat': 36.68278472716141, 'count': 212}\n",
      "117.21081309155257\n",
      "39.143929903310074\n",
      "{'city': '天津', 'lng': 117.21081309155257, 'lat': 39.143929903310074, 'count': 211}\n",
      "125.15014857862096\n",
      "42.98636494637781\n",
      "{'city': '西安', 'lng': 125.15014857862096, 'lat': 42.98636494637781, 'count': 193}\n",
      "87.56498774111579\n",
      "43.84038034721766\n",
      "{'city': '乌鲁木齐', 'lng': 87.56498774111579, 'lat': 43.84038034721766, 'count': 187}\n",
      "118.05913427786938\n",
      "36.80468485421204\n",
      "{'city': '淄博', 'lng': 118.05913427786938, 'lat': 36.80468485421204, 'count': 175}\n",
      "113.64964384986449\n",
      "34.756610064140254\n",
      "{'city': '郑州', 'lng': 113.64964384986449, 'lat': 34.756610064140254, 'count': 174}\n",
      "103.8233054407292\n",
      "36.06422552504259\n",
      "{'city': '兰州', 'lng': 103.8233054407292, 'lat': 36.06422552504259, 'count': 170}\n",
      "113.30764967515182\n",
      "23.12004910207623\n",
      "{'city': '广州', 'lng': 113.30764967515182, 'lat': 23.12004910207623, 'count': 151}\n",
      "126.65771685544611\n",
      "45.7732246332393\n",
      "{'city': '哈尔滨', 'lng': 126.65771685544611, 'lat': 45.7732246332393, 'count': 149}\n",
      "112.5508635890553\n",
      "37.89027705396754\n",
      "{'city': '太原', 'lng': 112.5508635890553, 'lat': 37.89027705396754, 'count': 148}\n",
      "121.30955503008511\n",
      "37.53656156285964\n",
      "{'city': '烟台', 'lng': 121.30955503008511, 'lat': 37.53656156285964, 'count': 146}\n",
      "120.38442818368189\n",
      "36.10521490127382\n",
      "{'city': '青岛', 'lng': 120.38442818368189, 'lat': 36.10521490127382, 'count': 143}\n",
      "112.9793527876505\n",
      "28.21347823085322\n",
      "{'city': '长沙', 'lng': 112.9793527876505, 'lat': 28.21347823085322, 'count': 134}\n",
      "108.29723355586638\n",
      "22.80649293560261\n",
      "{'city': '南宁', 'lng': 108.29723355586638, 'lat': 22.80649293560261, 'count': 123}\n",
      "106.7091770961758\n",
      "26.62990674144093\n",
      "{'city': '贵阳', 'lng': 106.7091770961758, 'lat': 26.62990674144093, 'count': 114}\n",
      "114.52208184420766\n",
      "38.048958314615454\n",
      "{'city': '石家庄', 'lng': 114.52208184420766, 'lat': 38.048958314615454, 'count': 113}\n",
      "117.28269909168304\n",
      "31.86694226068694\n",
      "{'city': '合肥', 'lng': 117.28269909168304, 'lat': 31.86694226068694, 'count': 107}\n",
      "117.08941491713698\n",
      "36.18807775894815\n",
      "{'city': '泰安', 'lng': 117.08941491713698, 'lat': 36.18807775894815, 'count': 103}\n",
      "115.89352754583604\n",
      "28.689578000141147\n",
      "{'city': '南昌', 'lng': 115.89352754583604, 'lat': 28.689578000141147, 'count': 95}\n",
      "120.2193754157201\n",
      "30.259244461536102\n",
      "{'city': '杭州', 'lng': 120.2193754157201, 'lat': 30.259244461536102, 'count': 81}\n",
      "111.6603505200542\n",
      "40.828318873081585\n",
      "{'city': '呼和浩特', 'lng': 111.6603505200542, 'lat': 40.828318873081585, 'count': 77}\n",
      "118.61264305187912\n",
      "37.408666288041196\n",
      "{'city': '东营', 'lng': 118.61264305187912, 'lat': 37.408666288041196, 'count': 75}\n",
      "118.34076823661057\n",
      "35.07240907439113\n",
      "{'city': '临沂', 'lng': 118.34076823661057, 'lat': 35.07240907439113, 'count': 73}\n",
      "114.31620010268132\n",
      "30.58108412692075\n",
      "{'city': '武汉', 'lng': 114.31620010268132, 'lat': 30.58108412692075, 'count': 73}\n",
      "125.31364242720072\n",
      "43.89833760709784\n",
      "{'city': '长春', 'lng': 125.31364242720072, 'lat': 43.89833760709784, 'count': 67}\n",
      "121.59347778143518\n",
      "38.94870993830429\n",
      "{'city': '大连', 'lng': 121.59347778143518, 'lat': 38.94870993830429, 'count': 66}\n",
      "109.84623853249181\n",
      "40.64711942570857\n",
      "{'city': '包头', 'lng': 109.84623853249181, 'lat': 40.64711942570857, 'count': 64}\n",
      "91.11189089598402\n",
      "29.662557062056536\n",
      "{'city': '拉萨', 'lng': 91.11189089598402, 'lat': 29.662557062056536, 'count': 63}\n",
      "114.0854909934702\n",
      "32.128582307511664\n",
      "{'city': '信阳', 'lng': 114.0854909934702, 'lat': 32.128582307511664, 'count': 59}\n",
      "123.43279092160505\n",
      "41.808644783515746\n",
      "{'city': '沈阳', 'lng': 123.43279092160505, 'lat': 41.808644783515746, 'count': 57}\n",
      "118.9307611921676\n",
      "42.297112320317325\n",
      "{'city': '赤峰', 'lng': 118.9307611921676, 'lat': 42.297112320317325, 'count': 56}\n",
      "115.68622865290865\n",
      "37.74692904585666\n",
      "{'city': '衡水', 'lng': 115.68622865290865, 'lat': 37.74692904585666, 'count': 55}\n",
      "122.09395836580605\n",
      "37.52878708125143\n",
      "{'city': '威海', 'lng': 122.09395836580605, 'lat': 37.52878708125143, 'count': 54}\n",
      "116.32816136356094\n",
      "37.46082592630501\n",
      "{'city': '德州', 'lng': 116.32816136356094, 'lat': 37.46082592630501, 'count': 54}\n",
      "117.03178985127641\n",
      "35.61576056625757\n",
      "{'city': '曲阜', 'lng': 117.03178985127641, 'lat': 35.61576056625757, 'count': 53}\n",
      "121.48789948569473\n",
      "31.24916171001514\n",
      "{'city': '上海', 'lng': 121.48789948569473, 'lat': 31.24916171001514, 'count': 52}\n",
      "106.2064786078384\n",
      "38.50262101187604\n",
      "{'city': '银川', 'lng': 106.2064786078384, 'lat': 38.50262101187604, 'count': 48}\n",
      "115.82093225904637\n",
      "32.90121133056961\n",
      "{'city': '阜阳', 'lng': 115.82093225904637, 'lat': 32.90121133056961, 'count': 48}\n",
      "114.04915354745555\n",
      "32.98315815409348\n",
      "{'city': '驻马店', 'lng': 114.04915354745555, 'lat': 32.98315815409348, 'count': 48}\n",
      "111.31098109196083\n",
      "30.73275781802591\n",
      "{'city': '宜昌', 'lng': 111.31098109196083, 'lat': 30.73275781802591, 'count': 47}\n",
      "118.10388604566381\n",
      "24.489230612469232\n",
      "{'city': '厦门', 'lng': 118.10388604566381, 'lat': 24.489230612469232, 'count': 47}\n",
      "119.33022110712668\n",
      "26.04712549657293\n",
      "{'city': '福州', 'lng': 119.33022110712668, 'lat': 26.04712549657293, 'count': 46}\n",
      "118.77807440802562\n",
      "32.05723550180587\n",
      "{'city': '南京', 'lng': 118.77807440802562, 'lat': 32.05723550180587, 'count': 45}\n",
      "115.46335977452752\n",
      "35.26244049607468\n",
      "{'city': '菏泽', 'lng': 115.46335977452752, 'lat': 35.26244049607468, 'count': 44}\n",
      "112.5428419005123\n",
      "33.0114195691165\n",
      "{'city': '南阳', 'lng': 112.5428419005123, 'lat': 33.0114195691165, 'count': 44}\n",
      "101.76792098980276\n",
      "36.640738611957964\n",
      "{'city': '西宁', 'lng': 101.76792098980276, 'lat': 36.640738611957964, 'count': 42}\n",
      "114.4826939323422\n",
      "36.60930792847117\n",
      "{'city': '邯郸', 'lng': 114.4826939323422, 'lat': 36.60930792847117, 'count': 41}\n",
      "119.50717994299387\n",
      "35.42022519314436\n",
      "{'city': '日照', 'lng': 119.50717994299387, 'lat': 35.42022519314436, 'count': 41}\n",
      "114.9359090792838\n",
      "25.84529553634676\n",
      "{'city': '赣州', 'lng': 114.9359090792838, 'lat': 25.84529553634676, 'count': 41}\n",
      "115.98686913929107\n",
      "36.455828514727976\n",
      "{'city': '聊城', 'lng': 115.98686913929107, 'lat': 36.455828514727976, 'count': 40}\n",
      "115.99984802155373\n",
      "29.71963952612237\n",
      "{'city': '九江', 'lng': 115.99984802155373, 'lat': 29.71963952612237, 'count': 40}\n",
      "114.52048681294414\n",
      "37.069531196911996\n",
      "{'city': '邢台', 'lng': 114.52048681294414, 'lat': 37.069531196911996, 'count': 39}\n",
      "114.89378153032918\n",
      "40.81118849110306\n",
      "{'city': '张家口', 'lng': 114.89378153032918, 'lat': 40.81118849110306, 'count': 39}\n",
      "109.42240181015114\n",
      "24.329053352467223\n",
      "{'city': '柳州', 'lng': 109.42240181015114, 'lat': 24.329053352467223, 'count': 39}\n",
      "112.09985718064648\n",
      "30.396103360852894\n",
      "{'city': '荆州', 'lng': 112.09985718064648, 'lat': 30.396103360852894, 'count': 38}\n",
      "117.27930538329689\n",
      "34.80788307838604\n",
      "{'city': '枣庄', 'lng': 117.27930538329689, 'lat': 34.80788307838604, 'count': 38}\n",
      "115.49481016907626\n",
      "38.88656454802744\n",
      "{'city': '保定', 'lng': 115.49481016907626, 'lat': 38.88656454802744, 'count': 37}\n",
      "118.18345059773411\n",
      "39.6505309225366\n",
      "{'city': '唐山', 'lng': 118.18345059773411, 'lat': 39.6505309225366, 'count': 37}\n",
      "117.0587387721073\n",
      "30.537897817381097\n",
      "{'city': '安庆', 'lng': 117.0587387721073, 'lat': 30.537897817381097, 'count': 36}\n",
      "105.73693162285642\n",
      "34.58431941886869\n",
      "{'city': '天水', 'lng': 105.73693162285642, 'lat': 34.58431941886869, 'count': 36}\n",
      "112.86733275750684\n",
      "35.49983446722556\n",
      "{'city': '晋城', 'lng': 112.86733275750684, 'lat': 35.49983446722556, 'count': 36}\n",
      "104.70551897529307\n",
      "31.50470125806144\n",
      "{'city': '绵阳', 'lng': 104.70551897529307, 'lat': 31.50470125806144, 'count': 36}\n",
      "114.65410194229541\n",
      "33.62374081814082\n",
      "{'city': '周口', 'lng': 114.65410194229541, 'lat': 33.62374081814082, 'count': 35}\n",
      "107.17064545238307\n",
      "34.36408080974784\n",
      "{'city': '宝鸡', 'lng': 107.17064545238307, 'lat': 34.36408080974784, 'count': 35}\n",
      "116.86380647644208\n",
      "38.297615350325714\n",
      "{'city': '沧州', 'lng': 116.86380647644208, 'lat': 38.297615350325714, 'count': 34}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120.30545590053595\n",
      "31.57003745192303\n",
      "{'city': '无锡', 'lng': 120.30545590053595, 'lat': 31.57003745192303, 'count': 34}\n",
      "114.35180650767198\n",
      "36.1102667221811\n",
      "{'city': '安阳', 'lng': 114.35180650767198, 'lat': 36.1102667221811, 'count': 34}\n",
      "113.91269016082377\n",
      "35.3072575576613\n",
      "{'city': '新乡', 'lng': 113.91269016082377, 'lat': 35.3072575576613, 'count': 33}\n",
      "122.40692581531741\n",
      "37.128686091876\n",
      "{'city': '荣成', 'lng': 122.40692581531741, 'lat': 37.128686091876, 'count': 33}\n",
      "117.96829241452845\n",
      "37.405313941825895\n",
      "{'city': '滨州', 'lng': 117.96829241452845, 'lat': 37.405313941825895, 'count': 32}\n",
      "114.29776983802476\n",
      "35.75542587422399\n",
      "{'city': '鹤壁', 'lng': 114.29776983802476, 'lat': 35.75542587422399, 'count': 32}\n",
      "107.04547762872542\n",
      "33.08156897815767\n",
      "{'city': '汉中', 'lng': 107.04547762872542, 'lat': 33.08156897815767, 'count': 32}\n",
      "124.33854311476908\n",
      "40.1290228266375\n",
      "{'city': '丹东', 'lng': 124.33854311476908, 'lat': 40.1290228266375, 'count': 31}\n",
      "112.44752476895121\n",
      "34.65736781765111\n",
      "{'city': '洛阳', 'lng': 112.44752476895121, 'lat': 34.65736781765111, 'count': 30}\n",
      "114.0259736573215\n",
      "22.546053546205247\n",
      "{'city': '深圳', 'lng': 114.0259736573215, 'lat': 22.546053546205247, 'count': 30}\n",
      "119.98186101345706\n",
      "31.771396744684242\n",
      "{'city': '常州', 'lng': 119.98186101345706, 'lat': 31.771396744684242, 'count': 30}\n",
      "117.93382245583847\n",
      "40.992521052457136\n",
      "{'city': '承德', 'lng': 117.93382245583847, 'lat': 40.992521052457136, 'count': 30}\n",
      "117.18810662317686\n",
      "34.27155343109188\n",
      "{'city': '徐州', 'lng': 117.18810662317686, 'lat': 34.27155343109188, 'count': 30}\n",
      "129.47130153101367\n",
      "43.05966660114006\n",
      "{'city': '延吉', 'lng': 129.47130153101367, 'lat': 43.05966660114006, 'count': 29}\n",
      "81.15601314780744\n",
      "40.349444301112534\n",
      "{'city': '阿克苏', 'lng': 81.15601314780744, 'lat': 40.349444301112534, 'count': 29}\n",
      "126.56454398883335\n",
      "43.87198833435933\n",
      "{'city': '吉林', 'lng': 126.56454398883335, 'lat': 43.87198833435933, 'count': 28}\n",
      "110.26092014748276\n",
      "25.262901245955238\n",
      "{'city': '桂林', 'lng': 110.26092014748276, 'lat': 25.262901245955238, 'count': 28}\n",
      "106.93126031648453\n",
      "27.699961377076246\n",
      "{'city': '遵义', 'lng': 106.93126031648453, 'lat': 27.699961377076246, 'count': 27}\n",
      "121.57900597258933\n",
      "29.885258965918055\n",
      "{'city': '宁波', 'lng': 121.57900597258933, 'lat': 29.885258965918055, 'count': 27}\n",
      "105.81968693999652\n",
      "32.441040158428244\n",
      "{'city': '广元', 'lng': 105.81968693999652, 'lat': 32.441040158428244, 'count': 27}\n",
      "81.2890480714925\n",
      "44.020355819309486\n",
      "{'city': '伊宁', 'lng': 81.2890480714925, 'lat': 44.020355819309486, 'count': 27}\n",
      "124.6990773926766\n",
      "46.07005100166291\n",
      "{'city': '大同', 'lng': 124.6990773926766, 'lat': 46.07005100166291, 'count': 26}\n",
      "129.608035395637\n",
      "44.58852115278252\n",
      "{'city': '牡丹江', 'lng': 129.608035395637, 'lat': 44.58852115278252, 'count': 26}\n",
      "120.87380095093022\n",
      "32.014664540823446\n",
      "{'city': '南通', 'lng': 120.87380095093022, 'lat': 32.014664540823446, 'count': 25}\n",
      "119.42777755116975\n",
      "32.40850525456844\n",
      "{'city': '扬州', 'lng': 119.42777755116975, 'lat': 32.40850525456844, 'count': 25}\n",
      "110.15167631614493\n",
      "22.643973608377276\n",
      "{'city': '玉林', 'lng': 110.15167631614493, 'lat': 22.643973608377276, 'count': 25}\n",
      "120.04288208342074\n",
      "49.27924545620167\n",
      "{'city': '海拉尔', 'lng': 120.04288208342074, 'lat': 49.27924545620167, 'count': 24}\n",
      "110.3308018483363\n",
      "20.022071276952243\n",
      "{'city': '海口', 'lng': 110.3308018483363, 'lat': 20.022071276952243, 'count': 24}\n",
      "113.14619551911636\n",
      "29.378007075474304\n",
      "{'city': '岳阳', 'lng': 113.14619551911636, 'lat': 29.378007075474304, 'count': 23}\n",
      "109.03804456347515\n",
      "32.70437044999437\n",
      "{'city': '安康', 'lng': 109.03804456347515, 'lat': 32.70437044999437, 'count': 23}\n",
      "102.64014734336779\n",
      "37.933172142905626\n",
      "{'city': '武威', 'lng': 102.64014734336779, 'lat': 37.933172142905626, 'count': 23}\n",
      "113.8353124597886\n",
      "34.026739588655204\n",
      "{'city': '许昌', 'lng': 113.8353124597886, 'lat': 34.026739588655204, 'count': 23}\n",
      "117.44115922145824\n",
      "40.00961366062446\n",
      "{'city': '蓟县', 'lng': 117.44115922145824, 'lat': 40.00961366062446, 'count': 23}\n",
      "130.28473458595482\n",
      "46.813779604740354\n",
      "{'city': '佳木斯', 'lng': 130.28473458595482, 'lat': 46.813779604740354, 'count': 23}\n",
      "111.00685365308033\n",
      "35.03885947981193\n",
      "{'city': '运城', 'lng': 111.00685365308033, 'lat': 35.03885947981193, 'count': 22}\n",
      "113.03770446779562\n",
      "25.782263975738925\n",
      "{'city': '郴州', 'lng': 113.03770446779562, 'lat': 25.782263975738925, 'count': 22}\n",
      "120.6906347337091\n",
      "28.002837594041246\n",
      "{'city': '温州', 'lng': 120.6906347337091, 'lat': 28.002837594041246, 'count': 21}\n",
      "120.59246738555154\n",
      "30.00236458052847\n",
      "{'city': '绍兴', 'lng': 120.59246738555154, 'lat': 30.00236458052847, 'count': 21}\n",
      "117.14761619529622\n",
      "35.06579087186232\n",
      "{'city': '滕州', 'lng': 117.14761619529622, 'lat': 35.06579087186232, 'count': 21}\n",
      "108.41343863669852\n",
      "30.71005418436643\n",
      "{'city': '万州', 'lng': 108.41343863669852, 'lat': 30.71005418436643, 'count': 21}\n",
      "120.61990711548988\n",
      "31.317987367952437\n",
      "{'city': '苏州', 'lng': 120.61990711548988, 'lat': 31.317987367952437, 'count': 21}\n",
      "116.70360222263986\n",
      "39.51861062508462\n",
      "{'city': '廊坊', 'lng': 116.70360222263986, 'lat': 39.51861062508462, 'count': 21}\n",
      "118.87584165150854\n",
      "28.95691044753569\n",
      "{'city': '衢州', 'lng': 118.87584165150854, 'lat': 28.95691044753569, 'count': 21}\n",
      "114.99203871092017\n",
      "27.113847650157084\n",
      "{'city': '吉安', 'lng': 114.99203871092017, 'lat': 27.113847650157084, 'count': 21}\n",
      "114.35362228468497\n",
      "30.56486029278519\n",
      "{'city': '武昌', 'lng': 114.35362228468497, 'lat': 30.56486029278519, 'count': 20}\n",
      "118.47018708791366\n",
      "36.68058442583128\n",
      "{'city': '青州', 'lng': 118.47018708791366, 'lat': 36.68058442583128, 'count': 20}\n",
      "115.0574069523237\n",
      "38.46583915804824\n",
      "{'city': '定州', 'lng': 115.0574069523237, 'lat': 38.46583915804824, 'count': 20}\n",
      "121.15277918828542\n",
      "29.99645671901067\n",
      "{'city': '余姚', 'lng': 121.15277918828542, 'lat': 29.99645671901067, 'count': 20}\n",
      "112.58381881071617\n",
      "26.89816441535812\n",
      "{'city': '衡阳', 'lng': 112.58381881071617, 'lat': 26.89816441535812, 'count': 19}\n",
      "112.25009284837394\n",
      "32.22916859153757\n",
      "{'city': '襄阳', 'lng': 112.25009284837394, 'lat': 32.22916859153757, 'count': 19}\n",
      "85.70941760173511\n",
      "41.705499905673676\n",
      "{'city': '库尔勒', 'lng': 85.70941760173511, 'lat': 41.705499905673676, 'count': 19}\n",
      "125.02183973021087\n",
      "46.596709020007594\n",
      "{'city': '大庆', 'lng': 125.02183973021087, 'lat': 46.596709020007594, 'count': 19}\n",
      "107.49497344658866\n",
      "31.214198858944734\n",
      "{'city': '达州', 'lng': 107.49497344658866, 'lat': 31.214198858944734, 'count': 19}\n",
      "103.78253888802674\n",
      "25.520758142870502\n",
      "{'city': '曲靖', 'lng': 103.78253888802674, 'lat': 25.520758142870502, 'count': 19}\n",
      "111.53878759640808\n",
      "36.09974544358536\n",
      "{'city': '临汾', 'lng': 111.53878759640808, 'lat': 36.09974544358536, 'count': 19}\n",
      "122.07322781023007\n",
      "41.14124802295616\n",
      "{'city': '盘锦', 'lng': 122.07322781023007, 'lat': 41.14124802295616, 'count': 19}\n",
      "117.35707986587582\n",
      "32.92949890669797\n",
      "{'city': '蚌埠', 'lng': 117.35707986587582, 'lat': 32.92949890669797, 'count': 19}\n",
      "109.517432662322\n",
      "30.30897851129638\n",
      "{'city': '恩施', 'lng': 109.517432662322, 'lat': 30.30897851129638, 'count': 18}\n",
      "119.91960601619071\n",
      "32.47605327483028\n",
      "{'city': '泰州', 'lng': 119.91960601619071, 'lat': 32.47605327483028, 'count': 18}\n",
      "119.31329513263806\n",
      "33.52834896694177\n",
      "{'city': '淮安', 'lng': 119.31329513263806, 'lat': 33.52834896694177, 'count': 18}\n",
      "121.96829072765755\n",
      "37.166083447280435\n",
      "{'city': '文登', 'lng': 121.96829072765755, 'lat': 37.166083447280435, 'count': 18}\n",
      "114.04606140022909\n",
      "33.57627868848315\n",
      "{'city': '漯河', 'lng': 114.04606140022909, 'lat': 33.57627868848315, 'count': 18}\n",
      "121.14774873823639\n",
      "41.13087887591732\n",
      "{'city': '锦州', 'lng': 121.14774873823639, 'lat': 41.13087887591732, 'count': 18}\n",
      "119.65257570368145\n",
      "29.10289910539069\n",
      "{'city': '金华', 'lng': 119.65257570368145, 'lat': 29.10289910539069, 'count': 17}\n",
      "120.06729564866686\n",
      "29.30644391183944\n",
      "{'city': '义乌', 'lng': 120.06729564866686, 'lat': 29.30644391183944, 'count': 17}\n",
      "109.16855802826001\n",
      "27.674902690624183\n",
      "{'city': '铜仁', 'lng': 109.16855802826001, 'lat': 27.674902690624183, 'count': 17}\n",
      "104.63301906152529\n",
      "28.76967479626591\n",
      "{'city': '宜宾', 'lng': 104.63301906152529, 'lat': 28.76967479626591, 'count': 17}\n",
      "115.16440991805027\n",
      "32.132798426221996\n",
      "{'city': '潢川', 'lng': 115.16440991805027, 'lat': 32.132798426221996, 'count': 17}\n",
      "113.12029208572514\n",
      "36.20166438574343\n",
      "{'city': '长治', 'lng': 113.12029208572514, 'lat': 36.20166438574343, 'count': 17}\n",
      "109.9869587958539\n",
      "27.557482901172786\n",
      "{'city': '怀化', 'lng': 109.9869587958539, 'lat': 27.557482901172786, 'count': 17}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "108.70750927819563\n",
      "34.34537299599856\n",
      "{'city': '咸阳', 'lng': 108.70750927819563, 'lat': 34.34537299599856, 'count': 16}\n",
      "120.86075764475588\n",
      "40.743029881317504\n",
      "{'city': '葫芦岛', 'lng': 120.86075764475588, 'lat': 40.743029881317504, 'count': 16}\n",
      "120.13724316328172\n",
      "30.877925155690896\n",
      "{'city': '湖州', 'lng': 120.13724316328172, 'lat': 30.877925155690896, 'count': 16}\n",
      "123.9872889421725\n",
      "47.34769981336638\n",
      "{'city': '齐齐哈尔', 'lng': 123.9872889421725, 'lat': 47.34769981336638, 'count': 16}\n",
      "100.21920895421849\n",
      "25.693966622473024\n",
      "{'city': '大理', 'lng': 100.21920895421849, 'lat': 25.693966622473024, 'count': 16}\n",
      "115.64188568785016\n",
      "34.43858864024636\n",
      "{'city': '商丘', 'lng': 115.64188568785016, 'lat': 34.43858864024636, 'count': 16}\n",
      "102.11788786037536\n",
      "27.863377395840185\n",
      "{'city': '西昌', 'lng': 102.11788786037536, 'lat': 27.863377395840185, 'count': 16}\n",
      "117.0345779137272\n",
      "39.457042575494015\n",
      "{'city': '武清', 'lng': 117.0345779137272, 'lat': 39.457042575494015, 'count': 16}\n",
      "121.66082212856833\n",
      "42.01925010706022\n",
      "{'city': '阜新', 'lng': 121.66082212856833, 'lat': 42.01925010706022, 'count': 16}\n",
      "113.13169534107074\n",
      "27.82743292766265\n",
      "{'city': '株洲', 'lng': 113.13169534107074, 'lat': 27.82743292766265, 'count': 16}\n",
      "76.01434279894337\n",
      "39.51311058531203\n",
      "{'city': '喀什', 'lng': 76.01434279894337, 'lat': 39.51311058531203, 'count': 16}\n",
      "118.38410842322828\n",
      "31.36601978754301\n",
      "{'city': '芜湖', 'lng': 118.38410842322828, 'lat': 31.36601978754301, 'count': 16}\n",
      "106.10555398379239\n",
      "30.800965168237234\n",
      "{'city': '南充', 'lng': 106.10555398379239, 'lat': 30.800965168237234, 'count': 15}\n",
      "109.50050975696925\n",
      "36.60332035226031\n",
      "{'city': '延安', 'lng': 109.50050975696925, 'lat': 36.60332035226031, 'count': 15}\n",
      "113.30084897797529\n",
      "33.74530145652439\n",
      "{'city': '平顶山', 'lng': 113.30084897797529, 'lat': 33.74530145652439, 'count': 15}\n",
      "119.6043676161184\n",
      "39.945461565897574\n",
      "{'city': '秦皇岛', 'lng': 119.6043676161184, 'lat': 39.945461565897574, 'count': 15}\n",
      "120.75134338102879\n",
      "36.905533169254866\n",
      "{'city': '莱阳', 'lng': 120.75134338102879, 'lat': 36.905533169254866, 'count': 15}\n",
      "125.94265013851434\n",
      "41.73639712986761\n",
      "{'city': '通化', 'lng': 125.94265013851434, 'lat': 41.73639712986761, 'count': 15}\n",
      "93.52937301238876\n",
      "42.34446710455244\n",
      "{'city': '哈密', 'lng': 93.52937301238876, 'lat': 42.34446710455244, 'count': 15}\n",
      "115.78792824511814\n",
      "33.87121056530193\n",
      "{'city': '亳州', 'lng': 115.78792824511814, 'lat': 33.87121056530193, 'count': 15}\n",
      "116.50525268298414\n",
      "31.75555835519844\n",
      "{'city': '六安', 'lng': 116.50525268298414, 'lat': 31.75555835519844, 'count': 15}\n",
      "119.70251223974057\n",
      "36.38731799231518\n",
      "{'city': '高密', 'lng': 119.70251223974057, 'lat': 36.38731799231518, 'count': 15}\n",
      "111.99639635657\n",
      "27.741073302349072\n",
      "{'city': '娄底', 'lng': 111.99639635657, 'lat': 27.741073302349072, 'count': 15}\n",
      "110.80122891676038\n",
      "32.636994339468124\n",
      "{'city': '十堰', 'lng': 110.80122891676038, 'lat': 32.636994339468124, 'count': 14}\n",
      "117.95546387714894\n",
      "28.457622553937274\n",
      "{'city': '上饶', 'lng': 117.95546387714894, 'lat': 28.457622553937274, 'count': 14}\n",
      "116.60079762482256\n",
      "35.40212166433135\n",
      "{'city': '济宁', 'lng': 116.60079762482256, 'lat': 35.40212166433135, 'count': 14}\n",
      "101.72242315249032\n",
      "26.587571257108653\n",
      "{'city': '攀枝花', 'lng': 101.72242315249032, 'lat': 26.587571257108653, 'count': 14}\n",
      "116.9886924118307\n",
      "33.63677238578078\n",
      "{'city': '宿州', 'lng': 116.9886924118307, 'lat': 33.63677238578078, 'count': 14}\n",
      "119.95942122689219\n",
      "36.24803145848278\n",
      "{'city': '胶州', 'lng': 119.95942122689219, 'lat': 36.24803145848278, 'count': 14}\n",
      "111.65371813684197\n",
      "29.012148855180815\n",
      "{'city': '常德', 'lng': 111.65371813684197, 'lat': 29.012148855180815, 'count': 14}\n",
      "102.20812626259014\n",
      "38.51607179953174\n",
      "{'city': '金昌', 'lng': 102.20812626259014, 'lat': 38.51607179953174, 'count': 14}\n",
      "123.77806236979248\n",
      "41.32583762664885\n",
      "{'city': '本溪', 'lng': 123.77806236979248, 'lat': 41.32583762664885, 'count': 14}\n",
      "119.92957584318849\n",
      "28.4562995521443\n",
      "{'city': '丽水', 'lng': 119.92957584318849, 'lat': 28.4562995521443, 'count': 14}\n",
      "120.49041120216108\n",
      "30.61234103032807\n",
      "{'city': '桐乡', 'lng': 120.49041120216108, 'lat': 30.61234103032807, 'count': 13}\n",
      "115.25847218770907\n",
      "40.632394360148936\n",
      "{'city': '宣化', 'lng': 115.25847218770907, 'lat': 40.632394360148936, 'count': 13}\n",
      "111.46152540355403\n",
      "27.236811244922414\n",
      "{'city': '邵阳', 'lng': 111.46152540355403, 'lat': 27.236811244922414, 'count': 13}\n",
      "121.42104597878073\n",
      "28.4005538171066\n",
      "{'city': '温岭', 'lng': 121.42104597878073, 'lat': 28.4005538171066, 'count': 13}\n",
      "118.32457035097686\n",
      "32.31735059538377\n",
      "{'city': '滁州', 'lng': 118.32457035097686, 'lat': 32.31735059538377, 'count': 13}\n",
      "117.69593963872306\n",
      "39.00214849843348\n",
      "{'city': '于家堡', 'lng': 117.69593963872306, 'lat': 39.00214849843348, 'count': 13}\n",
      "122.26036326322144\n",
      "43.633756072995986\n",
      "{'city': '通辽', 'lng': 122.26036326322144, 'lat': 43.633756072995986, 'count': 13}\n",
      "111.99518801957157\n",
      "37.02547627593959\n",
      "{'city': '介休', 'lng': 111.99518801957157, 'lat': 37.02547627593959, 'count': 13}\n",
      "107.94823859488538\n",
      "26.6317423326968\n",
      "{'city': '凯里', 'lng': 107.94823859488538, 'lat': 26.6317423326968, 'count': 13}\n",
      "117.01799673877318\n",
      "25.07868543351518\n",
      "{'city': '龙岩', 'lng': 117.01799673877318, 'lat': 25.07868543351518, 'count': 12}\n",
      "123.0077633288837\n",
      "41.118743682153465\n",
      "{'city': '鞍山', 'lng': 123.0077633288837, 'lat': 41.118743682153465, 'count': 12}\n"
     ]
    }
   ],
   "source": [
    "df_result = pd.DataFrame(columns=('city','lng','lat','count'))\n",
    "for indexs in cities_stat.index:\n",
    "    b = cities_stat.loc[indexs].values[0].strip() #将第一列city读取出来并清除不需要字符\n",
    "    c= cities_stat.loc[indexs].values[1]  #将第二列price读取出来并清除不需要字符\n",
    "    try:\n",
    "        lng = getlnglat(b)['result']['location']['lng'] #采用构造的函数来获取经度\n",
    "        lat = getlnglat(b)['result']['location']['lat'] #获取纬度\n",
    "        print(lng)\n",
    "        print(lat)\n",
    "        dict = {'city':b,'lng':lng,'lat':lat,'count':c}\n",
    "        print(dict)\n",
    "        df_result.loc[df_result.shape[0]+1] = dict\n",
    "    except:  \n",
    "        continue  \n",
    "df_result.to_csv(\"heat_map.csv\",index=False,encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#打开保存的带有经纬度的数据\n",
    "cities = pd.read_csv(dir + \"heat_map.csv\",sep=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cities.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import folium\n",
    "import webbrowser\n",
    "from folium.plugins import HeatMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#打开保存的带有经纬度的数据\n",
    "cities = pd.read_csv(dir + \"heat_map.csv\",sep=',')\n",
    "num = len(cities)-1\n",
    "lat = np.array(cities[\"lat\"][0:num])                        # 获取维度之维度值\n",
    "lon = np.array(cities[\"lng\"][0:num])                        # 获取经度值\n",
    "pop = np.array(cities[\"count\"][0:num],dtype=float)    # 获取人口数，转化为numpy浮点型\n",
    "data1 = [[lat[i],lon[i],pop[i]] for i in range(num)]    #将数据制作成[lats,lons,weights]的形式\n",
    "map_osm = folium.Map(location=[35,110],zoom_start=5)    #绘制Map，开始缩放程度是5倍\n",
    "HeatMap(data1).add_to(map_osm)  # 将热力图添加到前面建立的map里\n",
    "\n",
    "file_path = dir + \"heatmap.html\"\n",
    "map_osm.save(file_path)     # 保存为html文件\n",
    "\n",
    "webbrowser.open(file_path)  # 默认浏览器打开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "import matplotlib.pyplot as plt\n",
    "#有向图\n",
    "DG = nx.DiGraph()\n",
    "#添加一个节点\n",
    "DG.add_node('A')\n",
    "#作图，设置节点名显示,节点大小，节点颜色\n",
    "nx.draw(DG,with_labels=True,node_size=900,node_color = 'green')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#有向图\n",
    "DG = nx.DiGraph()\n",
    "#添加一个节点\n",
    "DG.add_node('A')\n",
    "DG.add_node('B')\n",
    "#添加边，有方向，A-->B\n",
    "DG.add_edge('A','B')\n",
    "#作图，设置节点名显示,节点大小，节点颜色\n",
    "nx.draw(DG,with_labels=True,node_size=900,node_color = 'green')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#以下两句是显示中文的方法 windows\n",
    "# from pylab import *\n",
    "# mpl.rcParams['font.sans-serif'] = ['SimHei']   #有效的方法\n",
    "\n",
    "# mac 下\n",
    "from pylab import mpl\n",
    "mpl.rcParams['font.sans-serif']=['SimHei']\n",
    "\n",
    "import networkx as nx\n",
    "import matplotlib.pyplot as plt\n",
    "# 设置matplotlib正常显示中文,用黑体显示中文\n",
    "plt.rcParams['font.sans-serif']=['SimHei']  \n",
    "plt.rcParams['axes.unicode_minus']=False \n",
    "colors = ['red', 'green', 'blue', 'yellow']\n",
    "#有向图\n",
    "DG = nx.DiGraph()\n",
    "#一次性添加多节点，输入的格式为列表\n",
    "DG.add_nodes_from(['甘肃', '武威', '兰州', '张掖','凉州区'])\n",
    "#添加边，数据格式为列表\n",
    "DG.add_edges_from([('甘肃', '武威'), ('甘肃', '兰州'), ('甘肃','张掖'),('武威','凉州区')])\n",
    "#作图，设置节点名显示,节点大小，节点颜色\n",
    "nx.draw(DG,with_labels=True, node_size=900, node_color = colors)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
